package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.ls.markt.model.admin.EnumManageItem;
import com.ls.markt.service.admin.EnumManageItemService;
import com.ls.markt.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/enumManageItem")
public class EnumManageItemController {

    @Autowired
    private EnumManageItemService enumManageItemService;

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject getItemsByEnumId(@RequestParam(value = "enumId",required = true)Long enumId){
        List<EnumManageItem> enumManageItems = enumManageItemService.listItemsByEnumId(enumId);
        JSONObject result = new JSONObject();
        result.put("total",enumManageItems.size());
        result.put("rows",enumManageItems);
        return result;
    }

    @GetMapping("/add/{enumId}")
    public ModelAndView add(@PathVariable("enumId")String enumId, Model model){
        model.addAttribute("enumId",enumId);
        return new ModelAndView("admin/enumManage/addItem","enumModel",model);
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id,Model model){
        EnumManageItem enumManageItem = enumManageItemService.getEnumManageItemById(id);
        model.addAttribute("enumManageItem",enumManageItem);
        return new ModelAndView("admin/enumManage/editItem","enumManageItemModel",model);
    }

    @PostMapping("/save")
    @ResponseBody
    public Response save(EnumManageItem enumManageItem){
        enumManageItemService.saveEnumManageItem(enumManageItem);
        return new Response(true);
    }

    /**
     * 执行删除枚举
     * @param id
     * @return
     */
    @DeleteMapping("/doDelete/{id}")
    @ResponseBody
    public Response doDelete(@PathVariable("id")Long id){
        enumManageItemService.removeEnumManageItem(id);
        return new Response(true);
    }

}
